// pages/addOrEditAddress/addOrEditAddress.js
import Toast from '@vant/weapp/toast/toast';
import {
  areaList
} from '../../utils/area-data'
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList: areaList,
    show: false,
    consignee: '',
    phone: "",
    provinceName: '',
    cityName: "",
    districtName: '',
    detail: '',
    label: '公司',
    allArea: '',
    updateId: '',
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const {
      id,
      type
    } = options
    if (type) {
      // console.log(id,type);
      this.setData({
        updateId: id,
        type
      })
      const {
        result
      } = await request({
        url: `/user/addressBook/getById/${id}`
      })
      // console.log(result);
      this.setData({
        consignee: result.consignee,
        phone: result.phone,
        provinceName: result.provinceName,
        cityName: result.cityName,
        districtName: result.districtName,
        detail: result.detail,
        label: result.label,
        allArea: `${result.provinceName}/${result.cityName}/${result.districtName}`
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChangeShow() {
    this.setData({
      show: true
    })
  },
  onCloseShow() {
    this.setData({
      show: false
    })
  },
  onGetArea(obj) {
    // console.log(obj);
    const {
      detail
    } = obj
    const {
      values
    } = detail
    this.setData({
      provinceName: values[0].name,
      cityName: values[1].name,
      districtName: values[2].name,
      allArea: `${values[0].name}/${values[1].name}/${values[2].name}`,
      show: false
    })

  },
  onTabs(e) {
    this.setData({
      label: e.detail.title
    })
  },
  async onSubmit() {
    const {
      consignee,
      phone,
      provinceName,
      cityName,
      districtName,
      detail,
      label,
      updateId
    } = this.data
    if (!consignee.trim() || !phone.trim() || !provinceName.trim() || !cityName.trim() || !districtName.trim() || !detail.trim())
      return Toast.fail({
        message: '请完整填写地址信息',
        forbidClick: true
      })
    // 2. 用正则判断手机号是否全为数字
    const phoneReg = /^\d+$/;
    if (!phoneReg.test(phone.trim())) {
      return Toast.fail({
        message: '手机号必须全为数字',
        forbidClick: true
      })
    }
    if (!this.data.type) {
      // 提交地址
      await request({
        method: 'post',
        url: '/user/addressBook/add',
        data: {
          consignee,
          phone,
          provinceName,
          cityName,
          districtName,
          detail,
          label
        }
      })
      wx.showToast({
        title: '成功添加',
        icon: 'success',
        duration: 1000,
        mask: true
      })
    } else {
      await request({
        method: "patch",
        url: '/user/addressBook/update',
        data: {
          consignee,
          phone,
          provinceName,
          cityName,
          districtName,
          detail,
          label,
          id: updateId
        }
      })
      wx.showToast({
        title: '成功修改',
        icon: 'success',
        duration: 1000,
        mask: true
      })
    }
    this.setData({
      consignee: '',
      phone: '',
      allArea: '',
      detail: '',
      label: ''
    })
    if(this.data.type){
      wx.navigateTo({
        url: '/pages/address/address',
      })
    }
  }
})